package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.pojo.UserTable;
import com.qf.service.UserTableService;
import com.qf.utils.ResUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lileijian
 * @create 2021-10-27 15:53
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserTableService userTableService;

    //分页查询
    @RequestMapping("findByPage")
    public ResUtil findByPage(@RequestParam(value = "page",required = false,defaultValue = "1")Integer pageNum,
                              @RequestParam(value = "limit",required = false,defaultValue = "3")Integer pageSize){
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //查询所有用户
        List<UserTable> list = userTableService.findByPage();
        //获取PageInfo分页对象
        PageInfo<UserTable> pageInfo = new PageInfo<>(list);
        //设置返回类型以及携带的参数
//        ResUtil jsonResultUtil = ResUtil.ok();
//        jsonResultUtil.setCount(pageInfo.getTotal());
//        jsonResultUtil.setData(pageInfo.getList());
        //此处不懂可以查看工具类 ResUtil 的 ok(Object data,Long count)
        //简写后：ResUtil.ok(pageInfo,pageInfo.getTotal())
        return ResUtil.ok(pageInfo.getList(),pageInfo.getTotal());
//        return jsonResultUtil;
    }

}
